package cn.itcast.itcaststore.web.servlet.manager;


import cn.itcast.itcaststore.domain.Notice;
import cn.itcast.itcaststore.serive.NoticeService;
import cn.itcast.itcaststore.serive.ProductService;
import cn.itcast.itcaststore.serive.impl.NoticeServiceImpl;
import cn.itcast.itcaststore.serive.impl.ProductServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


/**
 *	后台添加公告的servlet
 */
@WebServlet("/admin/AddNoticeServlet")
public class AddNoticeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NoticeService noticeService=new NoticeServiceImpl();
        Notice notice=new Notice();
        //获取表单数据
        Map<String, String[]> noticeMap = req.getParameterMap();
        try {
            BeanUtils.populate(notice,noticeMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //将当前时间设为添加公告的时间
        String s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        notice.setN_time(s);
        //调用addNotic方法
        try {
            noticeService.addNotice(notice);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        resp.sendRedirect(req.getContextPath() + "/admin/findNotice");
    }



    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doPost(req,res);
    }


}
